package com.tv.cms.system.role.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.tv.cms.system.role.model.Role;
import com.tv.cms.system.role.service.RoleService;
import com.tv.cms.utils.Result;
import com.tv.cms.utils.ResultCode;
import org.apache.ibatis.annotations.Select;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.tv.cms.system.admin.model.Admin;

/**
 * TODO
 *
 * @author
 * @date
 **/
@RestController
@RequestMapping("role")
public class RoleController {

    @Resource
    private RoleService service;

    @PostMapping("list")
    public Result<List<Role>> list(@RequestBody Role role){
        MPJLambdaWrapper<Role> wrapper = new MPJLambdaWrapper<Role>()
                .selectAll(Role.class)
                .selectAs("t1.user_name", Role:: getCreateName)
                .leftJoin(Admin.class, Admin :: getId, Role :: getCreateBy)
                .selectAs("t2.user_name", Role:: getUpdateName)
                .leftJoin(Admin.class, Admin :: getId, Role :: getUpdateBy)
                .like(StringUtils.hasText(role.getName()), Role :: getName, role.getName())
                .eq(role.getStatus() != null,  Role :: getStatus,role.getStatus())
                .orderByDesc(Role :: getStatus,Role :: getCreateTime);
        List<Role> list = service.selectJoinList(Role.class,wrapper);
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(),list);
    }

    @PostMapping("save")
    public Result<Object> save(@RequestBody Role role){
        if(!StringUtils.hasText(role.getName())){
            return new Result<>(ResultCode.FAILURE.getCode(), "请填写角色名", null);
        }
        role.setStatus(1);
        Role one = service.getOne(new QueryWrapper<Role>().eq("name", role.getName()));
        if(one != null){
            return new Result<>(ResultCode.FAILURE.getCode(), role.getName()+" 已经存在", null);
        }
        role.setUpdateBy(StpUtil.getLoginIdAsInt());
        role.setUpdateTime(new Date());
        boolean save;
        if(role.getId() == null){
            role.setCreateBy(StpUtil.getLoginIdAsInt());
            role.setCreateTime(new Date());
            save = service.save(role);
        } else {
            save = service.updateById(role);
        }
        return save ?  new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(),null) : new Result<>(ResultCode.FAILURE.getCode(), ResultCode.FAILURE.getMsg(),null);
    }

    /**
     * 更新角色状态
     * @param role
     * @return
     */
    @PutMapping("status")
    public Result status(@RequestBody Role role){
        Role r = new Role();
        r.setId(role.getId());
        r.setStatus(role.getStatus());
        boolean b = service.updateById(r);
        return b ? Result.OK() : Result.FAILURE();
    }
}
